package com.hugoo.imp.buzz;

import com.hugoo.core.Conf;
import com.hugoo.core.db.Conn;
import com.hugoo.core.db.Conn.DB_TYPE;
import com.hugoo.core.db.HSF;
import com.hugoo.core.db.mo.Record;
import com.hugoo.core.db.mo.RecordSet;
import com.hugoo.core.util.Logx;
import com.hugoo.core.util.TL;
import com.hugoo.imp.buzz.mo.ColumnInfo;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lovelock.Chan
 */
public abstract class FileInfo extends Logx {

    protected String _sid, col_title, table_name, scod, custCols;
    protected String[] _cols;
    protected boolean needCTable = true;
    protected List<ColumnInfo> col = new ArrayList<ColumnInfo>();
    protected String sys_sk = "__sys__";
    public static final String X__SID = "x__sid", X__SCOD = "x__scod", X__IMPDT = "x__impdt";
    protected DB_TYPE dbt;

    public FileInfo(String sid) {
        this(sid, null, null);
    }

    public FileInfo(String sid, String scod, String cust) {
        dbt = Conn.dbt(null);
        //----
        this._sid = sid;
        this.scod = scod;
        this.custCols = cust;
        initCol();
    }

    private void initCol() {
        table_name = getImpTableName();
        String tbColumn = getImpColumns();
        _cols = tbColumn.split(",");
        String cs[];
        StringBuilder sb = new StringBuilder();
        for (String cl : _cols) {
            cs = cl.split(":");
            if (cs.length != 2) {
                log.info("存在无效列配置【" + cl + "】！");
            } else {
                ColumnInfo ci = new ColumnInfo(cs[0], cs[1]);
                col.add(ci);//包装列
                ci.init_aLinfo(getImpColumn(ci.getName()));
                sb.append(cs[1]).append("|");
            }
        }
        col_title = sb.toString();//统一创建标题
        __buildSysColumn(col);//创建系统导入列
        RecordSet<Record> rs = HSF.query(null, getColumnInfoSql());
        while (rs.next()) {
            needCTable = false;
            String cl = rs.getString("COLUMN_NAME");
            for (ColumnInfo c : col) {
                if (c.getName().equalsIgnoreCase(cl)) {
                    c.setAltInfo(rs.getString("DATA_TYPE"), rs.getString("DATA_LENGTH"), rs.getString("DATA_DEFAULT"), rs.getString("NULLABLE"));
                }
            }
        }
    }

    protected String getTitle() {
        return Conf.getImpProp(_sid, Conf.IMP._imp_title);
    }

    protected String getColumnInfoSql() {
        return "SELECT T.COLUMN_NAME,T.DATA_TYPE,T.DATA_LENGTH,T.NULLABLE,T.DATA_DEFAULT,C.COMMENTS FROM USER_TAB_COLUMNS T,USER_COL_COMMENTS C "
                + "WHERE  C.COLUMN_NAME =T.COLUMN_NAME AND C.TABLE_NAME  =T.TABLE_NAME AND T.TABLE_NAME='" + getImpTableName().toUpperCase() + "'";
    }

    protected String getImpTableName() {
        return Conf.getImpProp(_sid, Conf.IMP._imp_table);
    }

    protected String getImpColumns() {
        if (TL.isNotEmpty(custCols)) {
            return custCols;
        } else if (TL.isNotEmpty(scod)) {
            RecordSet<Record> rs = HSF.query(null, "select IMP_COLUMN from t_sys_imp_filemeta where scod='" + scod + "' and sid='" + _sid + "'");
            if (rs.next()) {
                return rs.getString("IMP_COLUMN");
            }
        }
        return Conf.getImpProp(_sid, Conf.IMP._imp_table_column);
    }

    protected String getImpColumn(String key) {
        return Conf.getImpProp(_sid, Conf.IMP._imp_table_column + "." + key);
    }

    protected String getColumnTitle() {
        return col_title;
    }

    protected abstract void __buildSysColumn(List<ColumnInfo> col);
}
